﻿Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Globalization
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class PageStrip
        Inherits ScrollableItemStrip
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal itemSize As Size)
            MyBase.New(serviceProvider, Orientation.Horizontal, itemSize, Size.Empty)
        End Sub

        Protected Overrides Function CreateItemStrip(ByVal serviceProvider As IServiceProvider, ByVal orientation As Orientation, ByVal itemSize As Size, ByVal margin As Size) As ItemStrip
            Return New PageItemStrip(serviceProvider, orientation, itemSize, margin)
        End Function

        Public Overrides Sub Draw(ByVal graphics As Graphics)
            Dim container As GraphicsContainer = graphics.BeginContainer
            Dim bounds As Rectangle = MyBase.Bounds
            [Using] region As Region = New Region(New Rectangle(bounds.X, bounds.Y, (bounds.Width + 1), (bounds.Height + 1)))
                graphics.Clip = [region]
                MyBase.itemStrip.Draw(graphics)
                If (MyBase.itemStrip.ScrollPosition > 0) Then
                    Me.DrawButton(graphics, If((MyBase.Orientation = Orientation.Horizontal), ScrollButton.Left, ScrollButton.Up))
                End If
                If ((MyBase.itemStrip.ScrollPosition + MyBase.itemStrip.MaxVisibleItems) < MyBase.itemStrip.Items.Count) Then
                    Me.DrawButton(graphics, If((MyBase.Orientation = Orientation.Horizontal), ScrollButton.Right, ScrollButton.Down))
                End If
            End [Using]
            graphics.EndContainer(container)
        End Sub

        Private Sub DrawButton(ByVal graphics As Graphics, ByVal scrollButton As ScrollButton)
            Dim buttonBounds As Rectangle = MyBase.GetButtonBounds(scrollButton)
            If (MyBase.Orientation = Orientation.Horizontal) Then
                buttonBounds.Inflate((-MyBase.itemStrip.ItemSize.Width / 6), (-MyBase.itemStrip.ItemSize.Height / 4))
            Else
                buttonBounds.Inflate((-MyBase.itemStrip.ItemSize.Width / 4), (-MyBase.itemStrip.ItemSize.Height / 6))
            End If
            If (MyBase.ActiveButton = scrollButton) Then
                buttonBounds.Offset(1, 1)
                Dim size As Size = If((MyBase.Orientation = Orientation.Horizontal), New Size(0, 2), New Size(2, 0))
                buttonBounds.Inflate(size.Width, size.Height)
                graphics.FillRectangle(PageStrip.SelectionBrush, buttonBounds)
                graphics.DrawRectangle(Pens.Black, buttonBounds)
                buttonBounds.Inflate(-size.Width, -size.Height)
            End If
            [Using] path As GraphicsPath = ActivityDesignerPaint.GetScrollIndicatorPath(buttonBounds, scrollButton)
                graphics.FillPath(Brushes.Black, path)
                graphics.DrawPath(Pens.Black, path)
            End [Using]
        End Sub


        ' Fields
        Private Shared HighliteBrush As Brush = New SolidBrush(Color.FromArgb(100, &HFF, &HC3, &H6B))
        Private Shared SelectionBrush As Brush = New SolidBrush(Color.FromArgb(&HFF, &HC3, &H6B))

        ' Nested Types
        Private NotInheritable Class PageItemStrip
            Inherits ItemStrip
            ' Methods
            Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal orientation As Orientation, ByVal itemSize As Size, ByVal margin As Size)
                MyBase.New(serviceProvider, orientation, itemSize, margin)
            End Sub

            Public Overrides Sub Draw(ByVal graphics As Graphics)
                Dim container As GraphicsContainer = graphics.BeginContainer
                Dim bounds As Rectangle = MyBase.Bounds
                [Using] region As Region = New Region(New Rectangle(bounds.X, bounds.Y, (bounds.Width + 1), (bounds.Height + 1)))
                    graphics.Clip = [region]
                    Dim format As New StringFormat { _
                        .Alignment = StringAlignment.Center, _
                        .LineAlignment = StringAlignment.Center, _
                        .Trimming = StringTrimming.Character, _
                        .FormatFlags = StringFormatFlags.NoWrap _
                    }
                    Dim maxVisibleItems As Integer = MyBase.MaxVisibleItems
                    Dim scrollPosition As Integer = MyBase.ScrollPosition
                    Dim i As Integer = scrollPosition
                    Do While ((i < MyBase.Items.Count) AndAlso (i < (scrollPosition + maxVisibleItems)))
                        Dim itemInfo As ItemInfo = MyBase.Items.Item(i)
                        Dim itemBounds As Rectangle = MyBase.GetItemBounds(itemInfo)
                        Dim pageFoldSize As Integer = (itemBounds.Width / 5)
                        Dim pathArray As GraphicsPath() = ActivityDesignerPaint.GetPagePaths(itemBounds, pageFoldSize, DesignerContentAlignment.TopRight)
                        [Using] path As GraphicsPath = pathArray(0)
                            [Using] path2 As GraphicsPath = pathArray(1)
                                Dim white As Brush = Brushes.White
                                If (MyBase.SelectedItem Is itemInfo) Then
                                    white = PageStrip.SelectionBrush
                                ElseIf (MyBase.HighlitedItem Is itemInfo) Then
                                    white = PageStrip.HighliteBrush
                                End If
                                graphics.FillPath(white, path)
                                graphics.DrawPath(Pens.DarkBlue, path)
                                graphics.FillPath(Brushes.White, path2)
                                graphics.DrawPath(Pens.DarkBlue, path2)
                                If (itemInfo.Image Is Nothing) Then
                                    itemBounds.Y = (itemBounds.Y + pageFoldSize)
                                    itemBounds.Height = (itemBounds.Height - pageFoldSize)
                                    graphics.DrawString((i + 1).ToString(CultureInfo.CurrentCulture), Control.DefaultFont, SystemBrushes.ControlText, itemBounds, format)
                                Else
                                    itemBounds.Y = (itemBounds.Y + pageFoldSize)
                                    itemBounds.Height = (itemBounds.Height - pageFoldSize)
                                    itemBounds.X = (itemBounds.X + ((itemBounds.Width - itemBounds.Height) / 2))
                                    itemBounds.Width = itemBounds.Height
                                    itemBounds.Inflate(-2, -2)
                                    ActivityDesignerPaint.DrawImage(graphics, itemInfo.Image, itemBounds, DesignerContentAlignment.Center)
                                End If
                            End [Using]
                        End [Using]
                        i += 1
                    Loop
                End [Using]
                graphics.EndContainer(container)
            End Sub

        End Class
    End Class
End Namespace

